﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileTesterFormat.UserControls
{
    public partial class RuleFieldControl : UserControl
    {

        public void SetText(string line, string fieldDescription, int start, int stop)
        {
            try
            {
                if (!string.IsNullOrEmpty(line))
                {
                    var count = (stop - start) + 1;

                    if (stop == line.Length)
                    {
                        count = stop - start;
                    }

                    var result = line.Substring(start, count);

                    tbRulesText.Text = CheckIfAllAreSpaces(result);
                }

                lblRuleDescription.Text = fieldDescription;
            }
            catch(Exception ex)
            {
                lblRuleDescription.Text = "Error on parse " + ex.Message;
            }

            
            lblStart.Text = start.ToString();
            lblStop.Text = stop.ToString();
        }

        private string CheckIfAllAreSpaces(string result)
        {
            bool isWhiteSpaces = true;

            foreach (char c in result)
            {
                if (!Char.IsWhiteSpace(c))
                {
                    isWhiteSpaces = false;
                    break;
                }
            }

            if (isWhiteSpaces)
            {
                return result.Replace(" ", "$");
            }

            return result;
        }


        public RuleFieldControl()
        {
            InitializeComponent();
        }
    }
}
